package com.ccctop.cloud.service.impl;


import com.ccctop.cloud.mapper.TFundRequestDao;
import com.ccctop.cloud.service.api.TFundRequestService;
import com.ccctop.crowd.entity.po.TFundRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TFundRequest)表服务实现类
 *
 * @author makejava
 * @since 2022-02-20 17:38:26
 */
@Service("tFundRequestService")
public class TFundRequestServiceImpl implements TFundRequestService {
    @Resource
    private TFundRequestDao tFundRequestDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TFundRequest queryById(Integer id) {
        return this.tFundRequestDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<TFundRequest> queryAllByLimit(int offset, int limit) {
        return this.tFundRequestDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tFundRequest 实例对象
     * @return 实例对象
     */
    @Override
    public TFundRequest insert(TFundRequest tFundRequest) {
        this.tFundRequestDao.insert(tFundRequest);
        return tFundRequest;
    }

    /**
     * 修改数据
     *
     * @param tFundRequest 实例对象
     * @return 实例对象
     */
    @Override
    public TFundRequest update(TFundRequest tFundRequest) {
        this.tFundRequestDao.update(tFundRequest);
        return this.queryById(tFundRequest.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tFundRequestDao.deleteById(id) > 0;
    }

    @Override
    public List<TFundRequest> fundInfo(Integer userId) {
        TFundRequest query = new TFundRequest();
        query.setReviewerId(userId);
//        query.setStatus(0);
        return tFundRequestDao.queryAll(query);
    }

    @Override
    public void fundAgree(Integer itemId) throws Exception {
        boolean finish = tFundRequestDao.updateStatus(itemId,1)==1;
        if (!finish){
            throw new Exception("该资金提取请求已经处理过！");
        }
    }

    @Override
    public void fundRefuse(Integer itemId) throws Exception {
        boolean finish = tFundRequestDao.updateStatus(itemId,2)==1;
        if (!finish){
            throw new Exception("该资金提取请求已经处理过！");
        }
    }

    @Override
    public int saveRequests(List<TFundRequest> requests) {
        return tFundRequestDao.insertList(requests);
    }

    @Override
    public TFundRequest fundQuery(Integer itemId) {
        return tFundRequestDao.queryById(itemId);
    }
}